package records

import (
	"controllers"
	"github.com/astaxie/beego/orm"
	"library/common"
	"github.com/astaxie/beego"
)

type RecordsOptController struct {
	controllers.BaseRouter
}

func (c *RecordsOptController) RecordsList() {
	beego.Info("records")
	page, _ := c.GetInt("page", 0)
	start := 0
	length, _ := c.GetInt("length", 15)
	if page > 0 {
		start = (page - 1) * length
	}
	selectInfo := c.GetString("select_info")
	where := ""
	if selectInfo != "" {
		where = "  and( gj_history.operator LIKE '%" + selectInfo + "%' or gj_history.content LIKE '%" + selectInfo + "%'  or gj_history.type LIKE '%" + selectInfo + "%'  )"
	}
	var task []orm.Params
	o := orm.NewOrm()
	o.Raw("SELECT gj_history.* FROM `gj_history` LEFT JOIN `gj_script_record` ON gj_history.id = gj_script_record.task_id WHERE 1=1 "+where+" order by gj_script_record.id DESC  LIMIT ? ,?", start, length).Values(&task)
	var count []orm.Params
	total := 0
	o.Raw("SELECT count(gj_script_record.is_final) as total  FROM `gj_history` LEFT JOIN `gj_script_record` ON gj_history.id = gj_script_record.task_id WHERE 1=1 "+where).Values(&count)
	if len(count)>0{
		total = common.GetInt(count[0]["total"])
	}
	c.SetJson(200,  map[string]interface{}{"total": total, "currentPage": page, "table_data": task}, "")
	return
}